#!/bin/bash
set -e

info()   { echo -e -n "$@"; }
infoln() { echo -e -n "$@\n"; }
notice()   { echo -e -n "\033[01;32m$@\033[0m"; }
noticeln() { echo -e -n "\033[01;32m$@\n\033[0m"; }
error()    { echo -e -n "\033[01;31m$@\033[0m"; }
errorln()  { echo -e -n "\033[01;31m$@\n\033[0m"; }

get_latest() {
	repo=$1
	curl -Ls "https://api.github.com/repos/${repo}/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'
}

containerd_ver=$(get_latest containerd/containerd)
runc_ver=$(get_latest opencontainers/runc)
cni_ver=$(get_latest containernetworking/plugins)
nerdctl_ver=$(get_latest containerd/nerdctl)
crictl_ver=$(get_latest kubernetes-sigs/cri-tools)

noticeln "Installing containerd to /user/local"
curl -JLO https://github.com/containerd/containerd/releases/download/$containerd_ver/containerd-${containerd_ver:1}-linux-amd64.tar.gz
tar Cxzvf /usr/local containerd-${containerd_ver:1}-linux-amd64.tar.gz
mkdir -p /usr/local/lib/systemd/system/
noticeln "Starting containerd via systemd"
curl -L https://raw.githubusercontent.com/containerd/containerd/main/containerd.service -o /usr/local/lib/systemd/system/containerd.service
systemctl daemon-reload
systemctl enable --now containerd

noticeln "Installing runc.amd64 to /usr/local/sbin/runc"
curl -JLO https://github.com/opencontainers/runc/releases/download/$runc_ver/runc.amd64 
install -m 755 runc.amd64 /usr/local/sbin/runc
 
noticeln "Installing CNI"
curl -JLO https://github.com/containernetworking/plugins/releases/download/$cni_ver/cni-plugins-linux-amd64-$cni_ver.tgz
mkdir -p /opt/cni/bin
tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-$cni_ver.tgz

noticeln "Generate containerd config"
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
sed -i "s#k8s.gcr.io/pause#registry.aliyuncs.com/google_containers/pause#g"  /etc/containerd/config.toml
sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml
sed -i '/registry.mirrors]/a\ \ \ \ \ \ \ \ [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]' /etc/containerd/config.toml
sed -i '/registry.mirrors."docker.io"]/a\ \ \ \ \ \ \ \ \ \ endpoint = ["http://hub-mirror.c.163.com"]' /etc/containerd/config.toml
sed -i '/hub-mirror.c.163.com"]/a\ \ \ \ \ \ \ \ [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]' /etc/containerd/config.toml
sed -i '/"k8s.gcr.io"]/a\ \ \ \ \ \ \ \ \ \ endpoint = ["http://registry.aliyuncs.com/google_containers"]' /etc/containerd/config.toml

noticeln "Restart containerd to reload config"
systemctl restart containerd

noticeln "Installing nerdctl"
curl -JLO https://github.com/containerd/nerdctl/releases/download/$nerdctl_ver/nerdctl-${nerdctl_ver:1}-linux-amd64.tar.gz
tar xzvf nerdctl-${nerdctl_ver:1}-linux-amd64.tar.gz
/bin/cp -f nerdctl /usr/local/bin/
/bin/rm -rf containerd-rootless* nerdctl

noticeln "Installing crictl"
curl -JLO https://github.com/kubernetes-sigs/cri-tools/releases/download/$crictl_ver/crictl-$crictl_ver-linux-amd64.tar.gz
sudo tar zxvf crictl-$crictl_ver-linux-amd64.tar.gz -C /usr/local/bin
rm -f crictl-$crictl_ver-linux-amd64.tar.gz
crictl config runtime-endpoint unix:///run/containerd/containerd.sock
crictl config image-endpoint unix:///run/containerd/containerd.sock

# docker ....
# TODO: install docker 
# /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

